package com.interview.productsalesreports.service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.interview.productsalesreports.query.ProductSalesReportsConditionQuery;
import com.interview.productsalesreports.query.QueryResults;
import com.interview.productsalesreports.repository.ProductSalesReportsRepository;

@Service
public class ProductSalesReportsService {

	@Resource
	ProductSalesReportsRepository productSalesReportsRepository;

	public List<HashMap<String, Double>> monAmountAndYearAmount(String year,
			String mon) {
		return productSalesReportsRepository.monAmountAndYearAmount(year, mon);
	}

	public List productSalesValue(String year, String mon) {
		return productSalesReportsRepository.productSalesValue(year, mon);
	}

	public List AllcreateTime() {
		return productSalesReportsRepository.AllcreateTime();
	}

	// ----------------------------------------------------------------------------------------

	public Map<String, List<String>> yearAndMonForMap() {

		HashMap<String, List<String>> map = new HashMap<String, List<String>>();

		List<HashMap<String, String>> list = AllcreateTime();

		for (HashMap<String, String> hashMap : list) {

			String year = hashMap.get("year");
			String mon = hashMap.get("mon");

			if (!map.containsKey(year)) {
				List<String> mons = new ArrayList<String>();
				mons.add(mon);
				map.put(year, mons);
			} else {
				map.get(year).add(mon);
			}
		}

		return map;
	}

	// ----------------------------------------------------------------------
	// year format:"yyyy" mon format:"MM"
	public HashMap<String, Object> statisticsByYearAndMon(String year,
			String mon) {
		Map dateMap = yearAndMonForMap();// 数据库中所有订单 年月
		SimpleDateFormat yearFormat = new SimpleDateFormat("yyyy");
		SimpleDateFormat monFormat = new SimpleDateFormat("MM");

		String dateYear = yearFormat.format(new Date());
		String dateMon = monFormat.format(new Date());

		List results = new ArrayList<HashMap<String, String>>();
		List sumMap = new ArrayList<HashMap<String, String>>();

		if (year == "" || mon == "") {
			year = null;
			mon = null;
		}

		if (year == null & mon == null) {
			if (dateMap.containsKey(dateYear)) {
				List<String> monList = (List<String>) dateMap.get(dateYear);
				if (monList.contains(dateMon)) {

					results = productSalesValue(dateYear, dateMon);

					sumMap = monAmountAndYearAmount(dateYear, dateMon);
				} else {

					dateMon = monList.get(0);
					results = productSalesValue(dateYear, dateMon);

					sumMap = monAmountAndYearAmount(dateYear, dateMon);
				}
			} else {
				Set<String> keys = dateMap.keySet();
				dateYear = keys.iterator().next();// 第一个

				List<String> monList = (List<String>) dateMap.get(dateYear);
				dateMon = monList.get(0);
				results = productSalesValue(dateYear, dateMon);

				sumMap = monAmountAndYearAmount(dateYear, dateMon);
			}
		} else {
			results = productSalesValue(year, mon);

			sumMap = monAmountAndYearAmount(year, mon);
		}
		HashMap<String, Double> sum = (HashMap<String, Double>) sumMap.get(0);
		Double thatMonSum = sum.get("costsum");
		Double thatYearSum = sum.get("sums");

		HashMap<String, Object> map = new HashMap<String, Object>();

		map.put("list", results);
		map.put("dateYear", dateYear);
		map.put("dateMon", dateMon);

		map.put("thatMonSum", thatMonSum);
		map.put("thatYearSum", thatYearSum);

		return map;
	}

}
